package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bomp.facade.base.Header;
import cc.rengu.igas.bomp.facade.bean.RegionInfoBean;
import cc.rengu.igas.bomp.facade.request.RegionInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.RegionInfoQueryResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.BompRegionInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.BompRegionInfoQueryResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class BompRegionInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        BompRegionInfoQueryRequest request = new BompRegionInfoQueryRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BompRegionInfoQueryRequest bompRegionInfoQueryRequest = (BompRegionInfoQueryRequest) request;
        BizResponse bizResponse = new BizResponse();
        BompRegionInfoQueryResponse response = new BompRegionInfoQueryResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        String instId = bompRegionInfoQueryRequest.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(instId)) {
            rglog.error("必传参数instId法人号为空！", instId);
            bizResponse.setRespCode(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        List<cc.rengu.igas.bsps.facade.bean.RegionInfoBean> regionInfo = getRegionInfo(bompRegionInfoQueryRequest);
        response.setHeader(bompRegionInfoQueryRequest.getHeader());
        if (CollectionUtils.isEmpty(regionInfo)) {
            bizResponse.setResult(response);
            bizResponse.setRespCode(RespCodeEnum.REGION_INFO_QUERY_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.REGION_INFO_QUERY_ERROR.getRespDesc());
            return bizResponse;
        }
        response.setRegionInfoList(regionInfo);
        bizResponse.setResult(response);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        BompRegionInfoQueryResponse bompRegionInfoQueryResponse = (BompRegionInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(bompRegionInfoQueryResponse);
    }

    public List<cc.rengu.igas.bsps.facade.bean.RegionInfoBean> getRegionInfo(BompRegionInfoQueryRequest bompRegionInfoQueryRequest) throws Exception {
        String callMode = "1";
        callMode = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.APPLICATION_CONF_NODE, AppConfigConstant.BOMP_SERVICE_CALL_MODE);
        if (!StringUtil.isEmptyOrNull(callMode)) {
            if (!"0".equals(callMode.trim()) && !"1".equals(callMode.trim())) {
                rglog.error("内管相关信息获取调用模式<{}>参数配置错误:0-本地调用，1-微服务调用!", callMode.trim());
                throw new BizException("内管相关信息获取方式配置有误:<{}>", callMode);
            } else {
                callMode = callMode.trim();
            }
        }
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.info("正在获取行政地区信息列表,信息获取方式为<{}>", "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.HEADER_SRC_SYS_ID));
        header.setChanlId(ChannelEnum.UPMP.getChannelType());
        header.setTxnNum("M00BM029");
        header.setBizType("00");
//        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        RegionInfoQueryRequest regionInfoQueryRequest = new RegionInfoQueryRequest();
        regionInfoQueryRequest.setHeader(header);
        regionInfoQueryRequest.setRegionLevel(bompRegionInfoQueryRequest.getRegionLevel());
        regionInfoQueryRequest.setRegionName(bompRegionInfoQueryRequest.getRegionName());
        regionInfoQueryRequest.setSuperRegionCode(bompRegionInfoQueryRequest.getSuperRegionCode());
        regionInfoQueryRequest.setUserId(bompRegionInfoQueryRequest.getUserId());
        regionInfoQueryRequest.setPageIndex(bompRegionInfoQueryRequest.getPageIndex());
        regionInfoQueryRequest.setPageSize(bompRegionInfoQueryRequest.getPageSize());
        Result<RegionInfoQueryResponse> callResult = null;
        DubboService dubboService = new DubboServiceImpl();
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BOMP_PUBLIC_QUERY_SERVICE_INNER_PATH);
            callResult = (Result<RegionInfoQueryResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BOMP_REGION_INFO_METHOD, regionInfoQueryRequest);
        } else {
            callResult = (Result<RegionInfoQueryResponse>) dubboService.callDubboService(ShareServiceConstant.BOMP_PUBLIC_QUERY_SERVICE, ShareServiceConstant.BOMP_REGION_INFO_METHOD, regionInfoQueryRequest);
        }
        if (null == callResult || null == callResult.getResult()  || CollectionUtils.isEmpty(callResult.getResult().getRegionInfoList())) {
            rglog.info("查询行政地区信息失败");
            return null;
        }
        List<RegionInfoBean> regionInfoList = callResult.getResult().getRegionInfoList();
        List<cc.rengu.igas.bsps.facade.bean.RegionInfoBean> regionInfoBeanList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(regionInfoList)) {
            for (RegionInfoBean regionInfoBean : regionInfoList) {
                cc.rengu.igas.bsps.facade.bean.RegionInfoBean regionInfoBean1 = new cc.rengu.igas.bsps.facade.bean.RegionInfoBean();
                BeanUtil.beanCopy(regionInfoBean, regionInfoBean1);
                regionInfoBeanList.add(regionInfoBean1);
            }
        }
        return regionInfoBeanList;
    }

}
